package com.andnnl.jm;

/**
 * Created by chenss on 2022/9/26.
 */

import cn.hutool.core.codec.Base64;
import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.DES;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

public class DesUrlParamUtil {

    private static final ConcurrentHashMap<String, DES> desHashMap = new ConcurrentHashMap<>();

    private static DES getDes(String key) {
        return desHashMap.computeIfAbsent(key, k -> SecureUtil.des(Base64.encode(k).getBytes()));
    }


    /**
     * 加密
     *
     * @param key:密钥
     * @param encryptStr: 需加密字符串
     * @return: java.lang.String
     */
    public static String encrypt(String key, String encryptStr) {
        DES des = getDes(key);
        return des.encryptHex(encryptStr);
    }

    /**
     * 解密
     *
     * @param key:密钥
     * @param decryptStr: 解密字符串
     * @return: java.lang.String
     */
    public static String decrypt(String key, String decryptStr) {
        try {
            DES des = getDes(key);
            return des.decryptStr(decryptStr);
        } catch (Exception e) {
            return null;
        }
    }


    public static String encrypt2(String content, String slatKey, String vectorKey) throws Exception {
        //改了填充方式
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

        SecretKey secretKey = new SecretKeySpec(slatKey.getBytes(), "AES");

        IvParameterSpec iv = vectorKey == null ? null : new IvParameterSpec(vectorKey.getBytes());

        cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);

        byte[] encrypted = cipher.doFinal(content.getBytes());

        return Base64.encode(encrypted);
    }

    public static String encrypt3(String content, String slatKey, String vectorKey) throws Exception {
        //改了填充方式
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

        SecretKey secretKey = new SecretKeySpec(slatKey.getBytes(), "AES");

        IvParameterSpec iv = vectorKey == null ? null : new IvParameterSpec(vectorKey.getBytes());

        cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);

        byte[] encrypted = cipher.doFinal(content.getBytes());

        return ByteUtil.byteArray2HexString(encrypted);
    }

    public static void main(String[] args) throws Exception {
        String key = "asdfa23iuo";
        String sour = "asdfasdf啊lsdiflJK喜迎硘地莂顶置 不3蝗枯栽夺苛2222222222";
        System.out.println(sour);
        String rs = encrypt(key, sour);
        System.out.println("rs:\t\t\t\t"+rs);
        String base64rs = Base64.encodeUrlSafe(rs);
        System.out.println("base64rs:\t"+ base64rs);
        String base64sources= Base64.decodeStr(base64rs);
        System.out.println("base64sources:\t"+ base64sources);
        String s=decrypt(key, rs);
        System.out.println(s);

    }

}